package com.workcell.workdata.im.mvc.controller.message;


import com.workcell.workdata.im.annotation.Idempotent;
import com.workcell.workdata.im.annotation.UID;
import com.workcell.workdata.im.entity.Chat;
import com.workcell.workdata.im.entity.Message;
import com.workcell.workdata.im.model.response.ResponseEntity;
import com.workcell.workdata.im.model.vo.ChatVO;
import com.workcell.workdata.im.service.ChatService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/chat")
@Api(produces = "application/json", tags = "会话相关接口" )
@Validated
public class ChatController {

    @Resource
    private ChatService chatService;


    @Idempotent(expireTime = 10, info = "请勿重复新建会话")
    @ApiOperation(value = "添加会话")
    @PostMapping(value = "add")
    public ResponseEntity<?> add(@Valid @RequestBody Chat chat, @ApiIgnore @UID Long uid) {
        chat.setUid(uid);
        chatService.save(chat);
        return ResponseEntity.ok();
    }

    @ApiOperation(value = "查询会话")
    @GetMapping(value = "/list")
    public ResponseEntity<List<ChatVO>> list(@ApiIgnore @UID Long uid) {
        List<ChatVO> list = chatService.getList(uid);
        return ResponseEntity.ok(list);
    }

    @ApiOperation(value = "修改会话")
    @PostMapping(value = "update")
    public ResponseEntity<?> update(@Valid @RequestBody Chat chat,@ApiIgnore @UID Long uid) {
        chat.setUid(uid);
        chatService.update(chat);
        return ResponseEntity.ok();
    }


    @ApiOperation(value = "查询群聊所有记录")
    @GetMapping(value = "/groupMessageAll/{groupId}")
    public ResponseEntity<List<Message>> groupMessageAll(@PathVariable long groupId) {
        List<Message> messages = chatService.groupMessage(groupId);
        return ResponseEntity.ok(messages);
    }
}